﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FinApp.Controls.Lists {

    [TemplateVisualState(Name = "NormalState", GroupName = "CommonStates")]
    [TemplateVisualState(Name = "ChooseState", GroupName = "CommonStates")]
    public class ListBoxWithCheckBoxes : ListBox {

        #region DependencyProperties
        public static readonly DependencyProperty MinWidthForChooseStateProperty = DependencyProperty.Register( "MinWidthForChooseState", typeof(double), typeof(ListBoxWithCheckBoxes), new PropertyMetadata(new double(), OnMinWidthForChooseStatePropertyChanged)); /// TODO: Hacer min choose with
        public static readonly DependencyProperty WidthChooseAreaProperty = DependencyProperty.Register("WidthChooseArea", typeof(double), typeof(ListBoxWithCheckBoxes), new PropertyMetadata(50.0, OnWidthChooseAreaPropertyChanged));
        public static readonly DependencyProperty IsInChooseStateProperty = DependencyProperty.Register( "IsInChooseState", typeof(bool), typeof(ListBoxWithCheckBoxes), new PropertyMetadata(false, OnIsInChooseStatePropertyChanged)); 
        #endregion

        #region Static Methods
        public static double GetMinWidthForChooseState(DependencyObject theTarget) {
            return (double)theTarget.GetValue(MinWidthForChooseStateProperty);
        }

        public static void SetMinWidthForChooseState(DependencyObject theTarget, double theMinWidthForChooseState) {
            theTarget.SetValue(MinWidthForChooseStateProperty, theMinWidthForChooseState);
        }

        private static void OnMinWidthForChooseStatePropertyChanged(DependencyObject theTarget, DependencyPropertyChangedEventArgs theDependencyPropertyChangedEventArgs) {
            theTarget.SetValue(MinWidthForChooseStateProperty, theDependencyPropertyChangedEventArgs.NewValue);
        }

        private static void OnWidthChooseAreaPropertyChanged(DependencyObject theTarget, DependencyPropertyChangedEventArgs theDependencyPropertyChangedEventArgs) {
            theTarget.SetValue(WidthChooseAreaProperty, theDependencyPropertyChangedEventArgs.NewValue);
        }

        private static void OnIsInChooseStatePropertyChanged(DependencyObject theTarget, DependencyPropertyChangedEventArgs theDependencyPropertyChangedEventArgs) {
            if (theTarget != null && theTarget is ListBoxWithCheckBoxes) {
                ListBoxWithCheckBoxes aListBoxWithCheckBoxes = (ListBoxWithCheckBoxes)theTarget;
                if ((bool)theDependencyPropertyChangedEventArgs.NewValue) {
                    aListBoxWithCheckBoxes.SelectionMode = SelectionMode.Multiple;
                    aListBoxWithCheckBoxes.SelectedItems.Clear();
                    VisualStateManager.GoToState(aListBoxWithCheckBoxes, "ChooseState", true);
                } else {
                    aListBoxWithCheckBoxes.SelectionMode = SelectionMode.Single;
                    VisualStateManager.GoToState(aListBoxWithCheckBoxes, "NormalState", true);
                }
            }
        }
        #endregion

        #region Constructors
        public ListBoxWithCheckBoxes()
            : base() {
            this.DefaultStyleKey = typeof(ListBoxWithCheckBoxes);
            this.SelectionMode = SelectionMode.Single;

            this.Loaded += new RoutedEventHandler(ListBoxWithCheckBoxes_Loaded);
            this.SizeChanged += new SizeChangedEventHandler(ListBoxWithCheckBoxes_SizeChanged);
        }
        #endregion

        #region Properties
        public double MinWidthForChooseState {
            get { return (double)GetValue(MinWidthForChooseStateProperty); }
            set { SetValue(MinWidthForChooseStateProperty, value); }
        }

        public double WidthChooseArea {
            get { return (double)GetValue(WidthChooseAreaProperty); }
            set { SetValue(WidthChooseAreaProperty, value); }
        }

        public bool IsInChooseState {
            get { return (bool)GetValue(IsInChooseStateProperty); }
            set { SetValue(IsInChooseStateProperty, value); }
        } 
        #endregion

        #region Overrides
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
            base.OnMouseLeftButtonDown(e);
            Point p = e.GetPosition(this);
            if (p.X < this.WidthChooseArea) {
                this.IsInChooseState = true;
            }
        } 
        #endregion

        #region Events
        void ListBoxWithCheckBoxes_SizeChanged(object sender, SizeChangedEventArgs e) {
            SetMinWidthForChooseState(this, this.ActualWidth);
        }

        void ListBoxWithCheckBoxes_Loaded(object sender, RoutedEventArgs e) {
            SetMinWidthForChooseState(this, this.ActualWidth);
        } 
        #endregion
    }
}
